# Place all the behaviors and hooks related to the matching controller here.
# All this logic will automatically be available in application.js.
# You can use CoffeeScript in this file: http://coffeescript.org/
$(document).on 'page:change', ->
  $('#add-groups').click ->
    selected = $('#user_group_selector option:selected').remove()
    $('#user_group_ids').append(selected)

  $('#add-users').click ->
    selected = $('#user_selector option:selected').remove()
    $('#user_ids').append(selected)

  $('#remove-groups').click ->
    selected = $('#user_group_ids option:selected').remove()
    $('#user_group_selector').append(selected)

  $('#remove-users').click ->
    selected = $('#user_ids option:selected').remove()
    $('#user_selector').append(selected)

  $('#search-users button').click ->
    $('#user_selector').load("/users/search?s=#{$('#search-users input').val()}")

  $('#send').click ->
    $('#user_group_ids').val($('#user_group_ids option').map -> $(this).val())
    $('#user_ids').val($('#user_ids option').map -> $(this).val())

  $('.pause_delivery').click ->
    id = $(this).attr('delivery_id')
    $.patch "/deliveries/#{id}/pause", {'data-method':'patch'}, (result)->
      window.location.href = result

  $('.resume_delivery').click ->
    id = $(this).attr('delivery_id')
    $.patch "/deliveries/#{id}/resume", {'data-method':'patch'}, (result)->
      window.location.href = result

  $('.stop_delivery').click ->
    id = $(this).attr('delivery_id')
    $.patch "/deliveries/#{id}/stop", {'data-method':'patch'}, (result)->
      window.location.href = result

  $('.pause_mail').click ->
    id = $(this).attr('mail_id')
    $.patch "/promotion_mails/#{id}/pause", {'data-method':'patch'}, (result)->
      window.location.href = result

  $('.resume_mail').click ->
    id = $(this).attr('mail_id')
    $.patch "/promotion_mails/#{id}/resume", {'data-method':'patch'}, (result)->
      window.location.href = result

  $('.stop_mail').click ->
    id = $(this).attr('mail_id')
    $.patch "/promotion_mails/#{id}/stop", {'data-method':'patch'}, (result)->
      window.location.href = result